package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/28 21:34
 **/
public class Day27_3_Num151_ReverseWords {
    class Solution {
        public String reverseWords(String s) {
            StringBuffer sb = new StringBuffer();
            s = s.trim();
            String[] arr = s.split(" ");
            for(int i = arr.length-1; i >= 0; --i){
                if(arr[i] == " " || arr[i] == "") continue;
                sb.append(arr[i]);
                if(i == 0) break;
                sb.append(" ");
            }
            return sb.toString();
        }
    }
    class Solution2 {
        public String reverseWords(String s) {
            StringBuffer sb = new StringBuffer();
            s = s.trim();
            String[] data = s.split(" ");
            for(String str : data){
                if(str == " " || str == "") continue;
                sb.append((new StringBuffer(str).reverse()));
                sb.append(" ");
            }
            sb.deleteCharAt(sb.length()-1);
            return sb.reverse().toString();
        }
    }
}